package com.redsoft.spirit.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.redsoft.spirit.entity.Department;

/**
 * 部门管理的DAO类
 * 
 * <pre>
 * 部门管理的DAO类
 * </pre>
 * 
 * @author 杨雷
 * @since 1.0
 */
public interface DeptRepository extends BaseRepository<Department, String>{

	public Department getByDeptLevel(int level);
	
	@Query("from Department d where d.parentDeptId = ?1 and d.isActive = 1 order by d.displayOrder asc ")
	public List<Department> getByParentDeptId(String deptId);

	public Department getById(String deptId);

	public List<Department> getByIdNotAndIsActive(String deptId, int isActive);

	/**
	 * 根据用户id查询部门
	 * 
	 * @param id
	 * @return
	 */
	@Query("select d from Department d where d.id in (select de.deptId from DeptEmp de where de.empId in(select a.id from Account a where a.id =?1)) and d.isActive = 1")
	public Department getDeptByUserId(String id);
	
	@Query("select d from Department d where d.id in (select de.deptId from DeptEmp de where de.empId in(select a.id from Account a where a.id =?1)) and d.isActive = 1")
	public List<Department> getDeptsByUserId(String id);

	/**
	 * 获取园区服务部门信息
	 * 
	 * @return
	 */
	@Query("select d from Department d where d.deptType = '1' and d.isActive = 1")
	public List<Department> getServiceDept();

	public List<Department> getDeptByParentDeptId(String id);

	/**
	 * 删除部门.状态停用
	 * 
	 * @param id
	 */
	@Modifying
	@Query("update Department d set d.isActive = 0 where d.id = ?1")
	public void deleteDept(String id);
}
